package com.example.lib5.queue.test;

import java.util.Arrays;

/**
 * 创建日期：2022/6/3 0003 16:52
 *
 * @author tony.sun
 * 类说明：
 */
public class QueueB {
    public static void main(String[] args) {
        MyArrayQueueB myArrayQueueB = new MyArrayQueueB(5);
        System.out.println("遍历:");
        myArrayQueueB.showQueue();
        myArrayQueueB.addQueue(1);
        myArrayQueueB.addQueue(2);
        myArrayQueueB.addQueue(3);
        System.out.println("添加后再遍历");
        myArrayQueueB.showQueue();
        System.out.println("取出的值："+myArrayQueueB.takeOutQueue());
        System.out.println("取出的值："+myArrayQueueB.takeOutQueue());
        System.out.println("取出的值："+myArrayQueueB.takeOutQueue());
        System.out.println("取出的值："+myArrayQueueB.takeOutQueue());
        myArrayQueueB.showQueue();

    }
}
class MyArrayQueueB{
    int maxSize;
    int[] arr;
    int rear=-1;
    int front=-1;
    public MyArrayQueueB(int size) {
        this.maxSize=size;
        arr=new int[3];
    }

    public void showQueue() {
        if (isEmpty()){
            System.out.println("队列为空");
        }else {
            for (int i = front+1; i < rear+1; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    private boolean isEmpty() {
        return rear==front;
    }

    public void addQueue(int i) {
        if (isFull()){
            System.out.println("队列已经满了，无法添加");
            return;
        }else {
            rear++;
            arr[rear]=i;
        }
        System.out.println("队列的长度为……"+(rear-front));
    }

    private boolean isFull() {
        return rear==maxSize-1;
    }

    public int takeOutQueue() {
        //判断队列是否为空，空的话，就没有必要
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能再取数据");
        }else {
            front++;
            int i=arr[front];
            arr[front]=0;
            return i;
        }
    }
}
